package com.everflourish.yeah100.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.utils.constant.RecordType;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;

/**
 * 常用工具
 *
 * @author peifen
 * @date 2015-8-6 上午10:57:36
 */
public class CommonUtil {

    /**
     * 阅卷机蓝牙名称前缀
     */
    private static final String CR_S1300 = "S1300";
    private static final String[] CR_MECHINES = {CR_S1300};

    public static String formatDouble(Double d, String pattern) {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(d);
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param f
     * @return
     */
    public static String subZeroAndDot(Float f) {
        if (f == null) {
            return null;
        }
        String s = String.valueOf(f);
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    /**
     * MD5加码 生成32位md5码
     *
     * @param inputString the inputString to set
     * @return md5String
     * @since yeah100-services 1.0.0.0
     */
    public static String string2MD5(String inputString) {
        if (null == inputString) {
            inputString = "";
        }
        String MD5Str = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(inputString.getBytes());
            byte b[] = md.digest();

            int i;

            StringBuilder builder = new StringBuilder(32);
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    builder.append("0");
                builder.append(Integer.toHexString(i));
            }
            MD5Str = builder.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        }
        return MD5Str;
    }

    /**
     * 深读复制对象 复制后的对象跟原来的对象毫无关系
     *
     * @param t
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T deepCopy(T t) {
        ObjectInputStream ois;
        try {
            // 将该对象序列化成流,因为写在流里的是对象的一个拷贝，而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(t);
            // 将流序列化成对象
            ByteArrayInputStream bis = new ByteArrayInputStream(
                    bos.toByteArray());
            ois = new ObjectInputStream(bis);
            return (T) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 是否是长荣的阅卷机
     *
     * @param bluetoothName
     * @return
     */
    public static boolean isCRMachine(String bluetoothName) {
        if (bluetoothName == null) {
            return false;
        }
        for (String s : CR_MECHINES) {
            if (bluetoothName.contains(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据蓝牙的名称获取对应的蓝牙图片
     *
     * @param context
     * @param bluetoothName
     * @return
     */
    public static Drawable getBluetoothDrawable(Context context,
                                                String bluetoothName) {
        if (bluetoothName == null) {
            return null;
        }
        Drawable drawable = null;
        if (bluetoothName.contains(CR_S1300)) {
            drawable = ResourcesUtil.getDrawable(context, R.mipmap.cr_s1300);
        }
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
        }
        return drawable;
    }

    /**
     * 判断是否是help类型 read_failure_help,read_studen_id_failure_help,
     * not_found_student_id_help
     *
     * @param recordType
     * @return
     */
    public static boolean isHelpType(RecordType recordType) {
        if (recordType == RecordType.read_failure_help
                || recordType == RecordType.read_studen_id_failure_help
                || recordType == RecordType.not_found_student_id_help) {
            return true;
        } else {
            return false;
        }
    }
}
